#!/bin/bash
#by lingci
#2017/03/13

#============dissociation constant: a+b<-->c<-->d
paste Qball.dat Qfall.dat|awk '{printf(" %6.3f  %6.3f\n",$2,$4)}'>QiQf.dat
Ntotal=`cat QiQf.dat|wc -l`
Nubuf1=`awk '$1<=0.5 && $2<0.5' QiQf.dat|wc -l`
Nbuf2=`awk '$1>0.5 && $1<0.9 && $2<0.5 && $2>0.2' QiQf.dat|wc -l`
Nbf3=`awk '$1>0.6 && $2>0.6' QiQf.dat|wc -l`
Pubuf1=`echo "scale=3; $Nubuf1/$Ntotal" |bc`
Pbuf2=`echo "scale=3; $Nbuf2/$Ntotal" |bc`
Pbf3=`echo "scale=3; $Nbf3/$Ntotal" |bc`
KD=`echo "scale=4; ${Pubuf1}*${Pubuf1}/$Pbuf2" |bc`
ratio=`echo "scale=3; $Nbf3/$Nbuf2" |bc`
echo " Pubuf^2/Pbuf :" $KD
#echo "scale=4; ${Nubuf1}*${Nubuf1}/$Nbuf2"|bc
echo " Nbf/Nbuf :" $ratio

#============Prontein-RNA interaction Energy
#paste QiQf.dat Energy.dat > kankan.dat
#bound unfold state
awk '$1>0.5 && $1<0.9 && $2>0.2 && $2<0.5 {print $3}' kankan.dat > Coul-SR-buf2.dat
awk '$1>0.5 && $1<0.9 && $2>0.2 && $2<0.5 {print $4}' kankan.dat > LJ-SR-buf2.dat
awk '$1>0.5 && $1<0.9 && $2>0.2 && $2<0.5 {print $5}' kankan.dat > Coul-14-buf2.dat
awk '$1>0.5 && $1<0.9 && $2>0.2 && $2<0.5 {print $6}' kankan.dat > LJ-14-buf2.dat
#bound state
awk '$1>0.6 && $2>0.6 {print $3}' kankan.dat > Coul-SR-bf3.dat
awk '$1>0.6 && $2>0.6 {print $4}' kankan.dat > LJ-SR-bf3.dat
awk '$1>0.6 && $2>0.6 {print $5}' kankan.dat > Coul-14-bf3.dat
awk '$1>0.6 && $2>0.6 {print $6}' kankan.dat > LJ-14-bf3.dat
#calculate expectation
echo "====bound state======"
echo "Elect,native: "
cat Coul-14-bf3.dat |awk '{sum+=$1;count++}END{print sum/count}'
echo "Elect,non-native: "
cat Coul-SR-bf3.dat |awk '{sum+=$1;count++}END{print sum/count}'
echo "LJ,native: "
cat LJ-14-bf3.dat |awk '{sum+=$1;count++}END{print sum/count}'
echo "LJ,non-native: "
cat LJ-SR-bf3.dat |awk '{sum+=$1;count++}END{print sum/count}'
echo "====bound unfold state======"
echo "Elect,native: "
cat Coul-14-buf2.dat |awk '{sum+=$1;count++}END{print sum/count}'
echo "Elect,non-native: "
cat Coul-SR-buf2.dat |awk '{sum+=$1;count++}END{print sum/count}'
echo "LJ,native: "
cat LJ-14-buf2.dat |awk '{sum+=$1;count++}END{print sum/count}'
echo "LJ,non-native: "
cat LJ-SR-buf2.dat |awk '{sum+=$1;count++}END{print sum/count}'
